package com.unism.util;



import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.BitSet;

public class CByteUtil {
	public static boolean compareBytes(byte[] src, byte[] dest) {
		if (dest.length != src.length) {
			return false;
		}
		for (int i = 0; i < src.length; i++) {
			if (src[i] != dest[i]) {
				return false;
			}
		}
		return true;
	}

	public static byte[] subArray(byte[] param, long pos, long length) {
		byte[] buffer = new byte[(int) length];

		for (int i = 0; i < length; i++) {
			buffer[i] = param[((int) (pos + i))];
		}

		return buffer;
	}

	public static byte[] mergeArray(byte[] array1, byte[] array2) {
		byte[] buffer = new byte[array1.length + array2.length];

		for (int i = 0; i < array1.length; i++) {
			buffer[i] = array1[i];
		}

		for (int i = 0; i < array2.length; i++) {
			buffer[(i + array1.length)] = array2[i];
		}

		return buffer;
	}

	public static long toNumberic(byte[] param, long start, long length) {
		long result = 0L;

		for (int i = 0; i < length; i++) {
			result <<= 8;
			result |= param[((int) (start + i))] & 0xFF;
		}
		return result;
	}

	public static String toHexString(byte[] raw) {
		StringBuffer buffer = new StringBuffer();
		int j = 0;
		for (int i = 0; i < raw.length; i++) {
			int high = (raw[i] & 0xF0) >> 4;
			int low = raw[i] & 0xF;
			buffer.append(Integer.toHexString(high));
			buffer.append(Integer.toHexString(low));
			buffer.append(" ");
			j++;
			if (j >= 16) {
				buffer.append('\n');
				j = 0;
			}
		}
		return buffer.toString();
	}

	public static String toHexString2(byte[] raw) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < raw.length; i++) {
			int high = (raw[i] & 0xF0) >> 4;
			int low = raw[i] & 0xF;
			buffer.append(Integer.toHexString(high));
			buffer.append(Integer.toHexString(low));
			buffer.append(" ");
		}
		return buffer.toString();
	}

	public static String toHexString3(byte[] raw) {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < raw.length; i++) {
			int high = (raw[i] & 0xF0) >> 4;
			int low = raw[i] & 0xF;
			buffer.append(Integer.toHexString(high));
			buffer.append(Integer.toHexString(low));
		}
		return buffer.toString();
	}

	public static byte[] Int2byteAsDisk(int value, int len, int chan) {
		byte[] result = new byte[len];

		for (int i = 0; i < len; i++) {
			byte b = (byte) (value & 0xFF);
			result[i] = b;
			value >>= 8;
		}
		byte b0 = result[0];
		byte b1 = result[1];

		byte b2 = b0;
		b0 = b1;
		b1 = b2;
		result[0] = b0;
		result[1] = b1;
		result[(len - 1)] = ((byte) (chan & 0xFF));
		return result;
	}

	public static byte[] long2Byte(long value, int len) {
		byte[] result = new byte[len];

		for (int i = 0; i < len; i++) {
			result[(len - i - 1)] = ((byte) (int) (value & 0xFF));
			value >>= 8;
		}

		return result;
	}
	
	public static byte[] long2ByteConver(long value, int len) {
		byte[] result = new byte[len];

		for (int i = 0; i < len; i++) {
			result[(i)] = ((byte) (int) (value & 0xFF));
			value >>= 8;
		}

		return result;
	}

	public static long byte2Long(byte[] in, int start, int length) {
		long a = 0L;

		for (int i = start; i < start + length; i++) {
			a <<= 8;
			a += (in[i] & 0xFF);
		}

		return a;
	}

	public static byte[] int2byteforEmb(int value) {
		byte[] result = new byte[4];
		byte[] rst = new byte[4];

		result = long2Byte(value, 4);

		rst[0] = result[2];
		rst[1] = result[3];
		rst[2] = result[0];
		rst[3] = result[1];

		return rst;
	}

	public static int byte2intforEmb(byte[] value) {
		byte[] order = new byte[4];
		order[0] = value[2];
		order[1] = value[3];
		order[2] = value[0];
		order[3] = value[1];

		return (int) byte2Long(order, 0, 4);
	}

	public static int getIntBy1Byte(byte bb) {
		return (bb & 0xFF) << 0;
	}

	public static int getIntBy2Byte(byte[] bb) {
		return (bb[0] & 0xFF) << 8 | (bb[1] & 0xFF) << 0;
	}

	public static int getIntBy4Byte(byte[] bb) {
		return (bb[0] & 0xFF) << 8 | (bb[1] & 0xFF) << 0 | (bb[2] & 0xFF) << 24
				| (bb[3] & 0xFF) << 16;
	}

	public static byte[] get4ByteByInt(byte[] bb, int x) {
		bb[0] = ((byte) (x >> 8));
		bb[1] = ((byte) (x >> 0));
		bb[2] = ((byte) (x >> 24));
		bb[3] = ((byte) (x >> 16));
		return bb;
	}

	public static byte[] get2ByteByInt(byte[] bb, int x) {
		bb[0] = ((byte) (x >> 8));
		bb[1] = ((byte) (x >> 0));
		return bb;
	}

	public static byte[] get1ByteByInt(byte[] bb, int x) {
		bb[0] = ((byte) (x >> 0));
		return bb;
	}

	public static String byteToString(byte b) {
		byte high, low;
		byte maskHigh = (byte) 0xf0;
		byte maskLow = 0x0f;
		high = (byte) ((b & maskHigh) >> 4);
		low = (byte) (b & maskLow);
		StringBuffer buf = new StringBuffer();
		buf.append(findHex(high));
		buf.append(findHex(low));
		return buf.toString();
	}

	private static char findHex(byte b) {
		int t = new Byte(b).intValue();
		t = t < 0 ? t + 16 : t;
		if ((0 <= t) && (t <= 9)) {
			return (char) (t + '0');
		}
		return (char) (t - 10 + 'A');
	}

	public static byte[] hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF";
		char[] hexs = (hexStr.toUpperCase()).toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2];
		int n;

		for (int i = 0; i < bytes.length; i++) {
			n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 0xff);
		}
		return bytes;
	}

	public static String byte2bits(byte b) {
		int z = b;
		z |= 256;
		String str = Integer.toBinaryString(z);
		int len = str.length();
		return str.substring(len - 8, len);
	}

	public static BitSet fromByte(byte b) {
		BitSet bits = new BitSet(8);
		for (int i = 0; i < 8; i++) {
			bits.set(i, (b & 1) == 1);
			b >>= 1;
		}
		return bits;
	}

	// 31 38 36 32 31 32 31 33 30 35 30 30 31 34 31 00 6C 6F 67
	public static void main(String[] args) throws IOException {
		byte[] serialNum = { (byte) 0x31, (byte) 0x38, (byte) 0x36,
				(byte) 0x32, (byte) 0x31, (byte) 0x32, (byte) 0x31,
				(byte) 0x33, (byte) 0x30, (byte) 0x35, (byte) 0x30,
				(byte) 0x30, (byte) 0x31, (byte) 0x34, (byte) 0x31 };
		System.out.println(ParseDataType.parseByteArray2CharString(serialNum));

		byte[] softVersion = { (byte) 0x3F, (byte) 0xAF, (byte) 0x5C,
				(byte) 0x29 };

		byte[] hardVersion = { (byte) 0x40, (byte) 0x40, (byte) 0x00,
				(byte) 0x00 };

		System.out.println(ByteBuffer.wrap(softVersion).asFloatBuffer().get());
		System.out.println(ByteBuffer.wrap(hardVersion).asFloatBuffer().get());
		System.out.println(CByteUtil.getIntBy1Byte((byte) 0x13));
		System.out.println((byte) 0x13);

		System.out.println(byte2bits((byte) 0x11));
		System.out.println(fromByte((byte) 0x11));
		System.out.println(findHex((byte) 0x8A));
		String hexStr = "";
		byte[] hex  = hexStr2Str(hexStr);
		for (int i = 0; i < hex.length; i++) {
			System.out.println("-->"+hex[i]);
		}
		//00 05 7E 40
		//00 36 EE 80
		byte[] vv = CByteUtil.long2ByteConver(Long.valueOf(4294967295L).longValue(),4);
		//1F54
		for (int i = 0; i < vv.length; i++) {
			System.out.println(vv[i]);
		}
		byte[] devid = CByteUtil.long2Byte(3000, 2);
		for (int i = 0; i < devid.length; i++) {
			System.out.println(devid[i]);
		}
		
		byte[] cf = new byte[]{(byte)0xCF,(byte)0xCF,(byte)0x01};
		System.out.println(cf[0]);
		if (cf[0] == (byte)0xCF && cf[1] == (byte)0xCF){
			System.out.println(true);
		}
	}
	
	public static byte[] intToByteArray1(int i) {
		byte[] result = new byte[2];
		result[0] = (byte) (i % 256);
		result[1] = (byte) (i / 256);
		return result;
	}
}
